package com.smart.city.logger;

import org.openintents.sensorsimulator.hardware.SensorManagerSimulator;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorListener;
import android.hardware.SensorManager;

public class VirtualAccSensor implements Sensable {
	private Float[] value;
	private SensorManager sensorManager = null;
	private Sensor accSensor = null;
	private VirtualAccSensor instance;
	
	public VirtualAccSensor(Context context) {
		this.instance = this;
		this.value = new Float[] {0f, 0f, 0f};
		
		SensorManagerSimulator mSensorManager =  SensorManagerSimulator.getSystemService(context, Context.SENSOR_SERVICE);
		mSensorManager.connectSimulator();
		
		mSensorManager.registerListener(new SensorListener() {
			
			public void onAccuracyChanged(int sensor, int accuracy) {}

			
			public void onSensorChanged(int sensor, float[] values) {
				if (sensor == SensorManager.SENSOR_ACCELEROMETER) {
					synchronized (instance) {
						instance.value = new Float[3];
						instance.value[0] = values[0];
						instance.value[1] = values[1];
						instance.value[2] = values[2];
					}
				}	
			}
		},SensorManager.SENSOR_ACCELEROMETER, SensorManager.SENSOR_DELAY_UI);
	}

	
	public String getFormatedString() {
		Float[] tempSensorValue = getCurrentValue();
		String returnString = String.format("VS- /%.4f/%.4f/%.4f",
				tempSensorValue[0], tempSensorValue[1], tempSensorValue[2]);
		return returnString;
	}

	
	public Float[] getValue() {
		return getCurrentValue();
	}
	
	synchronized private Float[] getCurrentValue() {
		return this.value.clone();
	}
}
